package cn.mxb.logic.action.mxb;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

import cn.mxb.floor.pagination.Result;
import cn.mxb.floor.web.BaseAction;
import cn.mxb.logic.entity.SysCount;
import cn.mxb.logic.entity.SysTown;
import cn.mxb.logic.entity.SysUser;
import cn.mxb.logic.entity.SysVillage;
import cn.mxb.logic.entity.SysZu;
import cn.mxb.logic.entity.MxbBingli;
import cn.mxb.logic.entity.MxbBxRegister;
import cn.mxb.logic.entity.MxbPolicy;
import cn.mxb.logic.entity.MxbPrsinfo;
import cn.mxb.logic.service.ISysCountService;
import cn.mxb.logic.service.ISysTownService;
import cn.mxb.logic.service.ISysUserService;
import cn.mxb.logic.service.ISysVillageService;
import cn.mxb.logic.service.ISysZuService;
import unit.IdGenerator;
import unit.MyTime;
import cn.mxb.logic.service.IMxbBingliService;
import cn.mxb.logic.service.IMxbBxRegisterService;
import cn.mxb.logic.service.IMxbPolicyService;
import cn.mxb.logic.service.IMxbPrsinfoService;

@SuppressWarnings({ "serial", "rawtypes" })
public class MxbBingliAction extends BaseAction implements Preparable,ModelDriven {
	protected static final String List_JSP = "/WEB-INF/page/web/mxb/mxbBingliList.jsp";
	protected static final String ADD_JSP = "/WEB-INF/page/web/mxb/mxbBingliAdd.jsp";
	protected static final String UPDATE_JSP = "/WEB-INF/page/web/mxb/mxbBingliUpdate.jsp";
	protected static final String ERRORJSP = "/WEB-INF/page/web/mxb/errorList.jsp";
	protected static final String notmomty = "/WEB-INF/page/web/mxb/notmomty.jsp";
	protected static final String bxmoney = "/WEB-INF/page/web/mxb/bxmoney.jsp";
	protected static final String bxshagnxian = "/WEB-INF/page/web/mxb/bxshangxian.jsp";
	protected static final String notbx = "/WEB-INF/page/web/mxb/notbx.jsp";
	protected static final String nomoney ="/WEB-INF/page/web/mxb/notmomty.jsp";
	protected static final String wu ="/WEB-INF/page/web/mxb/wu.jsp";
	
	private MxbBingli mxbBingli;
	@Autowired
	private IMxbBingliService mxbBingliService;
	@Autowired
	private ISysCountService sysCountService;
	@Autowired
	private ISysTownService sysTownService;
	@Autowired
	private ISysVillageService sysVillageService;
	@Autowired
	private ISysZuService sysZuService;
	@Autowired
	private ISysUserService sysUserService;
	@Autowired
	private IMxbPolicyService mxbPolicyService;
	@Autowired
	private IMxbPrsinfoService mxbPrsinfoService;
	@Autowired
	private IMxbBxRegisterService mxbBxRegisterService;
	
	private Result<MxbBingli> pageResult;
	
	private List<MxbBingli> mxbBingliAllList; 
	private List<SysCount> sysCountAllList; 
	private List<SysTown> sysTownAllList; 
	private List<SysVillage> sysVillageAllList; 
	private List<SysZu> sysZuAllList; 
	private List<MxbPolicy> mxbPolicyAllList; 
	
	private Map<Integer,String> countNameMap = new HashMap<Integer, String>();    //县名
	private Map<Integer,String> townNameMap = new HashMap<Integer, String>();     //镇名
	private Map<Integer,String> villageNameMap = new HashMap<Integer, String>();  //村名
	private Map<Integer,String> zUidMap = new HashMap<Integer, String>(); 
	private Map<String,String> userNameMap = new HashMap<String, String>(); 
	private Map<String,String> mxbNameMap = new HashMap<String, String>(); 
	
	private String familyNameY; //参合家庭姓名搜索关键字
	private String userRealNameY; //报销人名字查询
	private String cardIdY;
	Double bxmoneyY;
	/**
	 * @return_type: String
	 * @file_name: 跳转添加界面
	 */
	public String openAdd(){
		mxbPolicyAllList =  mxbPolicyService.findAllList();
		setForwardView(ADD_JSP);
		return SUCCESS;
	}
	
	/**
	 * 添加
	 * @throws Exception 
	 * @return_type: String
	 */
	public String add() throws Exception{
		System.out.println("add");
		SysUser sysUser = new SysUser();
		if(cardIdY !=null && !"".equals(cardIdY)){
			if(sysUserService.findByProperty("cardId", cardIdY ) == null){
				sysUser = null;
				setForwardView(wu);
				return SUCCESS;
			}else{
				sysUser = sysUserService.findByProperty("cardId", cardIdY );
			}
		}
		if(sysUser !=null){
			if(sysUser.getUserId()!=null && !"".equals(sysUser.getUserId())){
				mxbBingli.setUserId(sysUser.getUserId());
			}
			//判断病人是否参合
			if(1 == mxbPrsinfoService.findByProperty("prsinfoId", sysUser.getUserId()).getPrsinfoState()){
				mxbBingli.setBxState(1);
			}else{
				mxbBingli.setBxState(0);
			}
		}
		mxbBingli.setStartday(MyTime.getNowDateTwo());
		IdGenerator id = new IdGenerator(1023);
		mxbBingli.setBingliId(id.getNextId().toString());
		mxbBingliService.add(mxbBingli);
		return openList();		
	}
	
	/**
	 * 病单报销
	 * @return
	 */
	public String binglibx(){
		MxbBxRegister mxbBxRegister = new MxbBxRegister();
		if(mxbBingli.getBingliId() !=null && !"".equals(mxbBingli.getBingliId()) ){
			mxbBxRegister.setBiliId(mxbBingli.getBingliId());
			System.out.println("报销病单号"+mxbBxRegister.getBiliId());
			//本次报销病单信息
			mxbBingli = mxbBingliService.findByProperty("bingliId", mxbBingli.getBingliId());
			mxbBxRegister.setBxUserId(mxbBingli.getUserId());
			System.out.println("报销人id"+mxbBxRegister.getBxUserId());
			//该病单是否报销，若未报销（1）则进行报销，已经报销（2），则跳转页面-提示报销完
			if(mxbBingli.getBxState() == 1){
				//根据病单信息查找人员信息
			       MxbPrsinfo mxbPrsinfo = new MxbPrsinfo();
			       mxbPrsinfo = mxbPrsinfoService.findByProperty("prsinfoId", mxbBingli.getUserId());
			       //判断该病单患者是否参合
			       System.out.println("参合吗？1参合"+mxbPrsinfo.getPrsinfoState());
			       if(mxbPrsinfo.getPrsinfoState() == 1){
			    	   //参合-查询参合年度政策信息
			    	   MxbPolicy mxbPolicy = new MxbPolicy();
			    	   mxbPolicy =   mxbPolicyService.findByProperty("policyId", mxbPrsinfo.getPolicyId());
			    	   System.out.println("报销比例="+mxbPolicy.getPolicyScale());
			    	   //该报销人是否有报销钱
			    	   if(mxbPrsinfo.getBxAllMoney() >0){
			    		 //根据病单花费与参合政策信息计算报销费用
				    	   if(mxbPolicy.getPolicyScale() !=null && !"".equals(mxbPolicy.getPolicyScale())){
				    		    //比例是
				    		    Double bili =  mxbPolicy.getPolicyScale();
				    		    bxmoneyY = mxbBingli.getBingliMoney() * bili;
				    		    System.out.println("报销金额"+bxmoneyY);
				    		    //本次报销金额比剩余报销金额小即可全部报销
				    		    if(bxmoneyY < mxbPrsinfo.getBxAllMoney() ){
				    		    	System.out.println("报销金额"+bxmoneyY);
				    		    	 mxbBxRegister.setBxMoney(bxmoneyY);
				    		         mxbBxRegister.setBxDate(MyTime.getStringDate());
				    		         mxbBxRegister.setBxCase(1);
				    		         //计算完报销金额，修改参合记录中的剩余报销金额
				    		         Double yue = mxbPrsinfo.getBxAllMoney() - bxmoneyY;
				    		         mxbPrsinfoService.updatetablebyproperty("MxbPrsinfo", "bxAllMoney", "prsinfoId", mxbBingli.getUserId(), yue);
				    		         try{
				    		        	 //报销完成后修改病单为已报销，并添加报销信息
				    		        	 mxbBingliService.updatetablebyproperty("MxbBingli", "bxState", "bingliId", mxbBingli.getBingliId(), 2);
				    		        	 System.out.println("修改参合-报销add开始");
				    		        	 mxbBxRegisterService.add(mxbBxRegister);
				    		        	 System.out.println("报销完成");
				    		         }catch(Exception e){
				    		        	 System.out.println(e);
				    		         }
					    		    setForwardView(bxmoney);
					    		    return SUCCESS;
				    		    }if(bxmoneyY > mxbPrsinfo.getBxAllMoney()){
				    		    	//本次报销金额不足以报销全部，则报销最后部分金额,并直接设置剩余报销金额为0
				    		         bxmoneyY = mxbBingli.getBingliMoney();
				    		         mxbBxRegister.setBxMoney(bxmoneyY);
				    		         mxbBxRegister.setBxDate(MyTime.getStringDate());
				    		         mxbBxRegister.setBxCase(1);
				    		         try{
				    		         mxbPrsinfoService.updatetablebyproperty("MxbPrsinfo", "bxAllMoney", "prsinfoId", mxbBingli.getUserId(), 0);
				    		        	 //报销完成后修改病单为已报销，并添加报销信息
				    		         mxbBingliService.updatetablebyproperty("MxbBingli", "bxState", "bingliId", mxbBingli.getBingliId(), 2);
				    		         mxbBxRegisterService.add(mxbBxRegister);
				    		         }catch(Exception e){
				    		        	 System.out.println(e);
				    		         }
				    		    	 setForwardView(bxshagnxian);
				    		    	 return SUCCESS;
				    		    }if(mxbPrsinfo.getBxAllMoney() == 0){
				    		    	setForwardView(nomoney);
				    		    	return SUCCESS;
				    		    }
				    	   }
			    	   }else{
			    		   setForwardView(notmomty);
			    		   return SUCCESS;
			    	   }
			       }
			}else{
				setForwardView(notbx);
				 return SUCCESS;
			}
			
			  
		}
		return SUCCESS;
		
		
	}
	
	
	/**
	 * @file_name 打开列表
	 * @return 
	 */
	public String openList(){
		try{
			if(mxbBingli.getUserId() !=null && !"".equals(mxbBingli.getUserId()) ){
				MxbPrsinfo mxbPrsinfo = new MxbPrsinfo();
				mxbPrsinfo = mxbPrsinfoService.findByProperty("prsinfoId", mxbBingli.getUserId());
				if(mxbPrsinfo.getPrsinfoState() == 0){
					setForwardView(ERRORJSP);
					return SUCCESS;
				}
			} 
			
			pageResult = mxbBingliService.find(mxbBingli, getPage(), getRow());
			for(MxbBingli mxbBingli : pageResult.getData()){
				if(null != mxbBingli.getUserId()  && !"".equals(mxbBingli.getUserId() )){
					userNameMap.put(mxbBingli.getUserId(), sysUserService.findByProperty("userId",mxbBingli.getUserId()).getUserRealName());
				}
				if(null != mxbBingli.getMxbId()  && !"".equals(mxbBingli.getMxbId() )){
					mxbNameMap.put(mxbBingli.getMxbId(), mxbPolicyService.findByProperty("mxbId",mxbBingli.getMxbId()).getMxbName());
				}
				System.out.println("xx"+mxbBingli.getBxState());
			}
			setForwardView(List_JSP);
			return SUCCESS;
		}catch(Exception e){
			System.out.println(e);
		}
		return SUCCESS;
	}
	
	/**
	 * @file_name:删除 
	 */
	public String delete(){
		mxbBingliService.delete(mxbBingli.getId());
		return openList();
	}
	
	/**
	 * @return_type: 
	 * @file_name:  打开修改页面
	 */
	public String openUpdate(){
		mxbBingli =  mxbBingliService.findByProperty("id",mxbBingli.getId()+"");
		sysCountAllList = sysCountService.findAllList();
		sysTownAllList = sysTownService.findAllList();
		sysVillageAllList = sysVillageService.findAllList();
		setForwardView(UPDATE_JSP);
		return SUCCESS;
	}
	
	/**
	 * 修改
	 * @return_type: 
	 */
	public String update(){
		mxbBingliService.add(mxbBingli);
		mxbBingli = null;
		return openList();
	}
	
	
	@Override
	public Object getModel() {
		return mxbBingli;
	}
	@Override
	public void prepare() throws Exception {
		if(mxbBingli ==null){
			mxbBingli = new MxbBingli();
		}
	}


	public MxbBingli getMxbBingli() {
		return mxbBingli;
	}

	public void setMxbBingli(MxbBingli mxbBingli) {
		this.mxbBingli = mxbBingli;
	}

	public Result<MxbBingli> getPageResult() {
		return pageResult;
	}

	public void setPageResult(Result<MxbBingli> pageResult) {
		this.pageResult = pageResult;
	}
	
	public List<SysCount> getSysCountAllList() {
		return sysCountAllList;
	}

	public void setSysCountAllList(List<SysCount> sysCountAllList) {
		this.sysCountAllList = sysCountAllList;
	}

	public List<SysTown> getSysTownAllList() {
		return sysTownAllList;
	}

	public void setSysTownAllList(List<SysTown> sysTownAllList) {
		this.sysTownAllList = sysTownAllList;
	}

	public Map<Integer, String> getCountNameMap() {
		return countNameMap;
	}

	public void setCountNameMap(Map<Integer, String> countNameMap) {
		this.countNameMap = countNameMap;
	}

	public Map<Integer, String> getTownNameMap() {
		return townNameMap;
	}

	public void setTownNameMap(Map<Integer, String> townNameMap) {
		this.townNameMap = townNameMap;
	}

	public Map<Integer, String> getVillageNameMap() {
		return villageNameMap;
	}

	public void setVillageNameMap(Map<Integer, String> villageNameMap) {
		this.villageNameMap = villageNameMap;
	}

	public List<SysVillage> getSysVillageAllList() {
		return sysVillageAllList;
	}

	public void setSysVillageAllList(List<SysVillage> sysVillageAllList) {
		this.sysVillageAllList = sysVillageAllList;
	}

	public List<MxbBingli> getMxbBingliAllList() {
		return mxbBingliAllList;
	}

	public void setMxbBingliAllList(List<MxbBingli> mxbBingliAllList) {
		this.mxbBingliAllList = mxbBingliAllList;
	}

	public List<SysZu> getSysZuAllList() {
		return sysZuAllList;
	}

	public void setSysZuAllList(List<SysZu> sysZuAllList) {
		this.sysZuAllList = sysZuAllList;
	}

	public Map<Integer, String> getzUidMap() {
		return zUidMap;
	}

	public void setzUidMap(Map<Integer, String> zUidMap) {
		this.zUidMap = zUidMap;
	}

	public String getFamilyNameY() {
		return familyNameY;
	}

	public void setFamilyNameY(String familyNameY) {
		this.familyNameY = familyNameY;
	}

	

	public String getUserRealNameY() {
		return userRealNameY;
	}

	public void setUserRealNameY(String userRealNameY) {
		this.userRealNameY = userRealNameY;
	}

	public String getCardIdY() {
		return cardIdY;
	}

	public void setCardIdY(String cardIdY) {
		this.cardIdY = cardIdY;
	}

	public List<MxbPolicy> getMxbPolicyAllList() {
		return mxbPolicyAllList;
	}

	public void setMxbPolicyAllList(List<MxbPolicy> mxbPolicyAllList) {
		this.mxbPolicyAllList = mxbPolicyAllList;
	}

	public Map<String, String> getUserNameMap() {
		return userNameMap;
	}

	public void setUserNameMap(Map<String, String> userNameMap) {
		this.userNameMap = userNameMap;
	}

	public Map<String, String> getMxbNameMap() {
		return mxbNameMap;
	}

	public void setMxbNameMap(Map<String, String> mxbNameMap) {
		this.mxbNameMap = mxbNameMap;
	}

	public Double getBxmoneyY() {
		return bxmoneyY;
	}

	public void setBxmoneyY(Double bxmoneyY) {
		this.bxmoneyY = bxmoneyY;
	}

	

	


	

	
	
}
